package view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import util.Constants;
import util.JNumberField;
import control.Supervisorio;
import entities.Variable;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class ActionWindow extends javax.swing.JFrame {
	private JPanel pnlAction;
	private JLabel lblAction1;
	private JButton btnSalvar;
	private JTextField txtName;
	private JTextField txtMsg;
	private JNumberField txtValue;
	private JLabel lblValue;
	private JLabel jLabel1;
	private JComboBox cmbMsgVariable;
	private JSeparator jSeparator5;
	private JTextField txtLogErro;
	private JLabel lblLogErro;
	private JSeparator jSeparator6;
	private JSeparator jSeparator4;
	private JSeparator jSeparator3;
	private JLabel lblMsg;
	private JPanel pnlLogErro;
	private JPanel pnlMsg;
	private JPanel pnSendMesg;
	private JPanel pnlSouth;
	private JLabel lblName;
	private JSeparator jSeparator2;
	private JComboBox cmbVar;
	private JLabel lblVar;
	private JPanel pnlVar;
	private JButton btnClose;
	private JPanel pnlButtons;
	private JLabel lblAction;
	private JSeparator jSeparator1;
	private JLabel lblCond;
	private JComboBox cmbActionType;
	private JLabel lblAction4;
	private JTextField txtControlVariable;
	private JComboBox cmbCondition;
	private JLabel lblAction2;
	private ComboBoxModel cmbVarModel;

	private ArrayList<Variable> variables;

	private Supervisorio supervisorio;

	private Variable currentVariable;

	public ActionWindow() {
		super();
		supervisorio = Supervisorio.getInstance();
		currentVariable = null;
		initGUI();
	}

	public ActionWindow(Variable variable) {
		super();
		supervisorio = Supervisorio.getInstance();
		currentVariable = variable;
		initGUI();
	}

	private void initGUI() {
		this.variables = supervisorio.getAllVariables();

		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			this.setTitle("i2m - Ação");
			{
				pnlAction = new JPanel();
				getContentPane().add(pnlAction, BorderLayout.CENTER);
				pnlAction.setLayout(null);
				pnlAction.setPreferredSize(new java.awt.Dimension(392, 165));
				{
					lblAction1 = new JLabel();
					pnlAction.add(lblAction1);
					lblAction1.setText("Se:");
					lblAction1.setBounds(12, 43, 28, 14);
				}
				{
					lblAction2 = new JLabel();
					pnlAction.add(lblAction2);
					lblAction2.setText("Valor Calculado");
					lblAction2.setBounds(69, 43, 100, 14);
				}
				{
					ComboBoxModel cmbAction1Model = new DefaultComboBoxModel(
							new String[] { "menor que", "maior que", "igual a",
									"diferente de" });
					cmbCondition = new JComboBox();
					pnlAction.add(cmbCondition);
					cmbCondition.setModel(cmbAction1Model);
					cmbCondition.setBounds(169, 40, 97, 21);
				}
				{
					txtControlVariable = new JTextField();
					pnlAction.add(txtControlVariable);
					txtControlVariable.setBounds(294, 40, 74, 21);
				}
				{
					lblAction4 = new JLabel();
					pnlAction.add(lblAction4);
					lblAction4.setText("Então:");
					lblAction4.setBounds(12, 135, 57, 14);
				}
				{
					ComboBoxModel cmbAction4Model = new DefaultComboBoxModel(
							new String[] { "Mostra mensagem", "Salvar em log",
									"Enviar valor à variael" });
					cmbActionType = new JComboBox();
					pnlAction.add(cmbActionType);
					cmbActionType.setModel(cmbAction4Model);
					cmbActionType.setBounds(79, 132, 144, 21);
					cmbActionType.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							cmbActionTypeMouseClicked(cmbActionType
									.getSelectedIndex());
						}
					});
				}
				{
					lblCond = new JLabel();
					pnlAction.add(lblCond);
					lblCond.setText("Condição");
					lblCond.setBounds(12, 12, 124, 19);
					lblCond.setFont(new java.awt.Font("Tahoma", 1, 14));
				}
				{
					jSeparator1 = new JSeparator();
					pnlAction.add(jSeparator1);
					jSeparator1.setBounds(12, 67, 356, 10);
				}
				{
					lblAction = new JLabel();
					pnlAction.add(lblAction);
					lblAction.setText("Ação");
					lblAction.setBounds(12, 77, 62, 15);
					lblAction.setFont(new java.awt.Font("Tahoma", 1, 14));
				}
				{
					jSeparator2 = new JSeparator();
					pnlAction.add(jSeparator2);
					jSeparator2.setBounds(12, 0, 356, 10);
				}
				{
					lblName = new JLabel();
					pnlAction.add(lblName);
					lblName.setText("Nome:");
					lblName.setBounds(12, 104, 41, 14);
				}
				{
					txtName = new JTextField();
					pnlAction.add(txtName);
					txtName.setBounds(79, 99, 144, 21);
				}
			}
			{
				pnlButtons = new JPanel();
				BorderLayout pnlButtonsLayout = new BorderLayout();
				pnlButtons.setLayout(pnlButtonsLayout);
				getContentPane().add(pnlButtons, BorderLayout.SOUTH);
				pnlButtons.setPreferredSize(new java.awt.Dimension(392, 118));
				{
					pnlSouth = new JPanel();
					FlowLayout pnlSouthLayout = new FlowLayout();
					pnlButtons.add(pnlSouth, BorderLayout.SOUTH);
					pnlSouth.setPreferredSize(new java.awt.Dimension(392, 32));
					pnlSouth.setLayout(pnlSouthLayout);
					{
						btnSalvar = new JButton();
						pnlSouth.add(btnSalvar);
						btnSalvar.setText("Salvar");
						btnSalvar.setPreferredSize(new java.awt.Dimension(93,
								21));
						btnSalvar.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) {
								btnSaveMouseClicked();
							}
						});
					}
					{
						btnClose = new JButton();
						pnlSouth.add(btnClose);
						btnClose.setText("Fechar");
						btnClose
								.setPreferredSize(new java.awt.Dimension(93, 21));
						btnClose.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) {
								btnCloseMouseClicked();
							}
						});
					}
				}
				{
					pnlMsg = new JPanel();
					pnlButtons.add(pnlMsg, BorderLayout.CENTER);
					pnlMsg.setSize(392, 86);
					pnlMsg.setLayout(null);
					pnlMsg.setVisible(false);
					{
						lblMsg = new JLabel();
						pnlMsg.add(lblMsg);
						lblMsg.setText("Mensagem:");
						lblMsg.setBounds(12, 20, 78, 14);
					}
					{
						txtMsg = new JTextField();
						pnlMsg.add(txtMsg);
						txtMsg.setBounds(79, 17, 288, 21);
						txtMsg.setText("waaa");
					}
					{
						jSeparator3 = new JSeparator();
						pnlMsg.add(jSeparator3);
						jSeparator3.setBounds(12, 7, 355, 10);
					}
				}
				{
					pnlLogErro = new JPanel();
					pnlButtons.add(pnlLogErro, BorderLayout.CENTER);
					pnlLogErro.setSize(392, 86);
					pnlLogErro.setLayout(null);
					pnlLogErro.setVisible(false);
					{
						lblLogErro = new JLabel();
						pnlLogErro.add(lblLogErro);
						lblLogErro.setText("Mensagem:");
						lblLogErro.setBounds(12, 20, 78, 14);
					}
					{
						txtLogErro = new JTextField();
						pnlLogErro.add(txtLogErro);
						txtLogErro.setBounds(79, 17, 288, 21);
					}
					{
						jSeparator5 = new JSeparator();
						pnlLogErro.add(jSeparator5);
						jSeparator5.setBounds(12, 7, 355, 10);
					}
				}
				{
					pnSendMesg = new JPanel();
					pnlButtons.add(pnSendMesg, BorderLayout.CENTER);
					pnSendMesg
							.setPreferredSize(new java.awt.Dimension(393, 86));
					pnSendMesg.setSize(392, 86);
					pnSendMesg.setLayout(null);
					pnSendMesg.setVisible(false);
					{
						jSeparator5 = new JSeparator();
						pnSendMesg.add(jSeparator5);
						jSeparator5.setBounds(12, 0, 368, 13);
					}
					{
						ComboBoxModel jComboBox1Model = new DefaultComboBoxModel(
								this.getVariablesName().toArray());
						cmbMsgVariable = new JComboBox();
						pnSendMesg.add(cmbMsgVariable);
						cmbMsgVariable.setModel(jComboBox1Model);
						cmbMsgVariable.setBounds(78, 9, 144, 21);
					}
					{
						jLabel1 = new JLabel();
						pnSendMesg.add(jLabel1);
						jLabel1.setText("Variavel:");
						jLabel1.setBounds(12, 12, 52, 14);
					}
					{
						lblValue = new JLabel();
						pnSendMesg.add(lblValue);
						lblValue.setText("Valor:");
						lblValue.setBounds(12, 42, 42, 14);
					}
					{
						txtValue = new JNumberField();
						pnSendMesg.add(txtValue);
						txtValue.setBounds(78, 42, 144, 21);
					}
				}
			}
			{
				pnlVar = new JPanel();
				getContentPane().add(pnlVar, BorderLayout.NORTH);
				pnlVar.setPreferredSize(new java.awt.Dimension(392, 33));
				pnlVar.setLayout(null);
				{
					lblVar = new JLabel();
					pnlVar.add(lblVar);
					lblVar.setText(" Variavel:");
					lblVar.setBounds(12, 8, 57, 14);
				}
				{
					cmbVarModel = new DefaultComboBoxModel(this
							.getVariablesName().toArray());
					cmbVar = new JComboBox();
					pnlVar.add(cmbVar);
					cmbVar.setModel(cmbVarModel);
					cmbVar.setBounds(68, 5, 144, 21);
					cmbVar.addActionListener(new ActionListener() {

						public void actionPerformed(ActionEvent e) {
							comboChangedItem();
						}

					});
				}
			}

			if (currentVariable != null)
				this.setCurrentOnCmb();

			this.comboChangedItem();
			pack();
			this.setSize(400, 343);
			this.setLocationRelativeTo(null);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void setCurrentOnCmb() {
		for (int i = 0; i < variables.size(); i++) {
			if (variables.get(i).getId() == currentVariable.getId()) {
				cmbVar.setSelectedIndex(i);
				break;
			}
		}

	}

	private void btnCloseMouseClicked() {
		this.dispose();
	}

	private void btnSaveMouseClicked() {
		String name = txtName.getText();
		double reference = Double.parseDouble(txtControlVariable.getText());
		int actionType = cmbActionType.getSelectedIndex();
		String operation = (String) cmbCondition.getSelectedItem();
		String result = txtMsg.getText();

		// if (pnlMsg.isVisible()) {
		// System.out.println("visible");
		// int index = cmbMsgVariable.getSelectedIndex();
		// long id = variables.get(index).getId();
		// System.out.println("index: " + index + " id: " + id);
		// result += id + "|";
		// }

		System.out.println(result);

		supervisorio.createAction(currentVariable, name, reference, actionType,
				result, operation);

	}

	private ArrayList<String> getVariablesName() {

		ArrayList<String> names = new ArrayList<String>();

		for (Variable variable : variables) {
			names.add(variable.getName());
		}

		return names;

	}

	private void cmbActionTypeMouseClicked(int selectedIndex) {
		if (selectedIndex == Constants.ACTION_SHOW_MESSAGE) {
			pnlLogErro.setVisible(false);
			pnSendMesg.setVisible(false);
			pnlMsg.setVisible(true);
		} else if (selectedIndex == Constants.ACTION_SAVE_LOG) {
			pnlMsg.setVisible(false);
			pnSendMesg.setVisible(false);
			pnlLogErro.setVisible(true);
		} else if (selectedIndex == Constants.ACTION_SEND_VARIABLE_VALUE) {
			pnlMsg.setVisible(false);
			pnlLogErro.setVisible(false);
			pnSendMesg.setVisible(true);
		} else {
			pnlMsg.setVisible(false);
			pnlLogErro.setVisible(false);
		}
		this.repaint();
	}

	private void comboChangedItem() {
		int index = cmbVar.getSelectedIndex();
		currentVariable = variables.get(index);
	}
}
